var fs = require('fs');
var student = require('../../model/student');

function index(app){
    var config = app.config;
    var swig = app.swig;

	//读取D盘所有文件,测试非阻塞
    path = 'D:';
    function explorer(path){
        fs.readdir(path, function(err, files){
            //err 为错误 , files 文件名列表包含文件夹与文件
            if(err){
                console.log('error:\n' + err);
                return;
            }

            files.forEach(function(file){

                fs.stat(path + '/' + file, function(err, stat){
                    if(err){console.log(err); return;}
                    if(stat.isDirectory()){                 
                        // 如果是文件夹遍历
                        explorer(path + '/' + file);
                    }else{
                        // 读出所有的文件
                        console.log('文件名:' + path + '/' + file);
                        body = '文件名:' + path + '/' + file;
                    }               
                });
                
            });

        });
    }
    explorer(path);

    var html = swig.renderFile(app.view_path + '/home/index.html', {
        pagetitle: config.APP_NAME,
        body : '主页'
    });

    app.resHtml(html);
}

function lists(app){
    student.selectStudent(function(data){
        console.log(data);

        html = 'name:'+ data[0].name + ' ; age:' + data[0].age + ' ; email:' + data[0].email;
        app.resHtml(html);
    });
}

exports.index = index;
exports.lists = lists;